#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@NAME          : rename.py
@TIME          : 2024/12/03 17:23:00
@AUTHOR        : chenlip
@VERSION       : 0.0.1
@DESCRIPTION   : 用于定义数据库表结构
'''
from sqlalchemy import Column, Integer, String, Text, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship

MBase = declarative_base()

class FileInfo(MBase):
    __tablename__ = 'fileinfo'
    __table_args__ = {'comment': '文件信息表'}  # 表描述
    
    id = Column(Integer, primary_key=True, autoincrement=True, comment='自增id')
    path = Column(String, nullable=False, comment='mp4目录下的文件路径')
    srcname = Column(String, nullable=False, comment='原文件名')
    dstname = Column(String, nullable=False, comment='新文件名')
    description = Column(Text, nullable=True, comment='文件介绍')
    tags = Column(Text, nullable=True, comment='文件标签')
    mcurl = Column(String, nullable=True, comment='minio文件存储位置')
    ctime = Column(String, nullable=False, comment='创建时间')
    mtime = Column(String, nullable=False, comment='修改时间')
    size = Column(Integer, nullable=False, comment='文件大小')
    status = Column(Integer, default=0, comment='文件状态（1-正常，0-删除）')
    note = Column(Text, nullable=True, comment='备注')

    # 定义关系
    file_metas = relationship("FileMeta", back_populates="fileinfo")

    def __repr__(self):
        return f'<FileInfo {self.id} {self.srcname} {self.dstname} {self.description} {self.tags} {self.mcurl} {self.ctime} {self.mtime} {self.size} {self.status} {self.note}>'

class FileMeta(MBase):
    __tablename__ = 'filemeta'
    __table_args__ = {'comment': '文件元数据表'}  # 表描述
    
    id = Column(Integer, primary_key=True, autoincrement=True, comment='自增id')
    fileinfoid = Column(Integer, ForeignKey('fileinfo.id'), nullable=False, comment='文件信息表id')
    key = Column(String, nullable=False, comment='元数据key')
    value = Column(String, nullable=False, comment='元数据value')

    # 定义反向关系
    fileinfo = relationship("FileInfo", back_populates="file_metas")

    def __repr__(self):
        return f'<FileMeta {self.id} {self.fileinfoid} {self.key} {self.value}>'

